<!-- 待提交运费 -->
<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :search.sync="searchForm"
      v-model="form" ref="crud" @search-change="searchChange" @search-reset="searchReset"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad"
      @row-update="rowUpdate" @selection-change="selectionChange" @sort-change="sortChange">
      <template slot="menuLeft">
        <el-button type="primary" :disabled="selection.length === 0" size="mini" @click.stop="confirm">确认提交</el-button>
        <el-button type="primary" size="mini" @click="exportExcel()">导出</el-button>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <!-- <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="view(row.parentId)"
          >费用明细</el-button
        > -->
        <el-button type="text" size="mini" icon="el-icon-view" @click.stop="viewDetail(row)">详情</el-button>
        <el-button type="text" size="mini" icon="el-icon-view" v-if="row.reason"
          @click.stop="viewReason(row.reason)">失败原因</el-button>
      </template>
    </avue-crud>
    <expenseDetails ref="expenseDetails" />
    <demandDetail ref="demandDetail" />
    <tranportDetail ref="tranportDetail" />
  </basic-container>
</template>

<script>
import {
  getList,
  confirmShipping,
  uploadCertificate,
} from "@/api/financial/index";
import expenseDetails from "./expenseDetails";
import demandDetail from "../transportation/components/demandDetail";
import tranportDetail from "../transportation/components/tranportDetail";
import { getToken } from "@/util/auth";
import { serialize } from "@/util/util";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selection: [],
      selectionRule: "",
      selectionDocCurrency: "", //当前选中的币别
      selectionCarrierNo: "", //当前选中的承运商
      selecttionChargeText: "", //当前选中的费用类型
      selecttionWerks: "", //当前选中的收发工厂
      // ZFO1、ZFO2、ZFO3、ZFO4、ZFO5、ZFO6、ZFO7、ZFO8、ZFO9、ZFOA、ZFOB
      data: [],
      searchForm: {},
    };
  },
  components: {
    expenseDetails,
    demandDetail,
    tranportDetail,
  },
  computed: {
    option() {
      return {
        height: "auto",
        calcHeight: 30,
        tip: false,
        addBtn: false,
        delBtn: false,
        editTitle: "上传凭证",
        updateBtnText: "保存",
        editBtnText: "上传凭证",
        selection: true,
        selectable: (row) => {
          if (this.selectionRule === "") {
            return true;
          } else {
            return (
              this.selectionRule.indexOf(row.freightOrderType) !== -1 &&
              row.docCurrency === this.selectionDocCurrency &&
              row.carrierNo === this.selectionCarrierNo &&
              row.werks === this.selecttionWerks &&
              (this.selecttionChargeText === "运杂费"
                ? row.chargeText === "运杂费"
                : row.chargeText !== "运杂费")
            );
          }
        },
        reserveSelection: true,
        searchShow: true,
        searchLabelWidth: 100,
        index: true,
        searchSpan: 6,
        searchMenuSpan: 4,
        border: true,
        labelWidth: "120",
        menuWidth: "250",
        column: [
          {
            label: "货运订单",
            prop: "freightOrder",
            search: true,
            width: 100,
            editDisplay: false,
          },
          {
            label: "需求单号",
            prop: "demandOrderNumber",
            width: 120,
            editDisplay: false,
          },
          // {
          //   label: "运单号",
          //   prop: "shippingNoteNumber",
          //   search: true,
          // },
          // {
          //   label: "货运订单",
          //   prop: "freightOrder",
          //   search: true,
          // },
          {
            label: "货运订单类型",
            prop: "freightOrderType",
            type: "select",
            width: 100,
            searchLabelWidth: "100",
            editDisplay: false,
            props: {
              label: "value",
              value: "key",
            },
            search: true,
            dicUrl: `/api/blade-tms/common/SelectEnum?name=torType`,
            addDisplay: false,
          },
          // {
          //   label: "交货单",
          //   prop: "deliverNo",
          //   search: true,
          // },
          {
            label: "承运商",
            prop: "carrierName",
            width: 120,
            editDisplay: false,
            search: true,
          },
          {
            label: "结算类型",
            prop: "orderType",
            width: 100,
            editDisplay: false,
          },
          // {
          //   label: "运费类型",
          //   prop: "chargeType",
          // },
          // {
          //   label: "过账状态",
          //   prop: "fsdPost",
          // },
          // {
          //   label: "运费",
          //   prop: "money",
          // },
          // {
          //   label: "结算单号",
          //   prop: "no",
          // },
          {
            label: "费用类型",
            prop: "chargeText",
            width: 100,
            editDisplay: false,
          },
          {
            label: "运费",
            prop: "money",
            editDisplay: false,
            width: 100,
          },
          {
            label: "币别",
            prop: "docCurrency",
            width: 100,
            editDisplay: false,
          },
          {
            label: "源位置街道",
            prop: "sourceLocationStreet",
            width: 150,
            editDisplay: false,
          },
          {
            label: "目标位置街道",
            prop: "destinationStreet",
            width: 150,
            editDisplay: false,
          },
          {
            label: "物料信息",
            prop: "materialInformation",
            width: 100,
            editDisplay: false,
          },
          {
            label: "车牌号",
            prop: "licensePlateNumber",
            width: 100,
            editDisplay: false,
          },
          {
            label: "司机",
            prop: "driver",
            width: 100,
            editDisplay: false,
          },
          {
            label: "交货单号",
            prop: "deliverNo",
            width: 100,
            editDisplay: false,
          },
          {
            label: "提单号",
            prop: "billOfLadingNumber",
            width: 100,
            editDisplay: false,
          },
          {
            label: "源位置名称",
            prop: "startLocationName",
            width: 100,
            editDisplay: false,
          },
          {
            label: "客户名称",
            prop: "customerName",
            width: 100,
            editDisplay: false,
          },
          {
            label: "签收数量",
            prop: "num",
            editDisplay: false,
            width: 100,
          },
          {
            label: "合同公司",
            width: 100,
            prop: "contractCompany",
            editDisplay: false,
          },
          {
            label: "上传凭证",
            prop: "uploadVoucher",
            width: 100,
            hide: true,
            showColumn: false,
            type: "upload",
            span: 24,
            listType: "picture-card",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传凭证",
                trigger: "change",
              },
            ],
          },
        ],
      };
    },
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query, { staging: true });
      this.loading = true;
      getList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
          this.$refs.crud.clearSort();
        });
      });
    },
    //编辑
    rowUpdate(row, index, done, loading) {
      uploadCertificate({ id: row.id, uploadVoucher: row.uploadVoucher }).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    confirm() {
      this.$confirm(`是否确认运费?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          confirmShipping({
            ids: this.selection
              .map((item) => {
                return item.id;
              })
              .join(","),
          }).then(
            () => {
              this.$refs.crud.toggleSelection();
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => { });
    },
    view(id) {
      this.$refs.expenseDetails.open(id);
    },
    viewReason(val) {
      this.$alert(val, "失败原因", {
        showCancelButton: true,
        showConfirmButton: false,
        cancelButtonText: "关闭",
      });
    },
    viewDetail(row) {
      if (row.orderType === "运输单") {
        this.$refs.tranportDetail.open(row.parentId, row.orderType);
      } else {
        this.$refs.demandDetail.open(row.parentId, row.orderType);
      }
    },
    //多选
    selectionChange(list) {
      let arr = [
        "ZFO1、ZFO3、ZFO4、ZFB1、ZFOC、ZFOD、ZFOE",
        "ZFO6",
        "ZFO2",
        "ZFO5",
        "ZFO7、ZFO8、ZFO9、ZFOA、ZFOB、ZFB2",
      ];
      if (list.length !== 0) {
        let freightOrderType = list[0].freightOrderType;
        this.selectionDocCurrency = list[0].docCurrency;
        this.selectionCarrierNo = list[0].carrierNo;
        this.selecttionWerks = list[0].werks;
        this.selecttionChargeText = list[0].chargeText;
        for (let i in arr) {
          if (arr[i].indexOf(freightOrderType) !== -1) {
            this.selectionRule = arr[i];
          }
        }
      } else {
        this.selectionRule = "";
        this.selectionDocCurrency = "";
        this.selectionCarrierNo = "";
        this.selecttionWerks = "";
        this.selecttionChargeText = "";
      }

      this.selection = list;
      for (let i in list) {
        if (
          this.selectionRule.indexOf(list[i].freightOrderType) === -1 ||
          list[i].docCurrency !== this.selectionDocCurrency ||
          list[i].carrierNo !== this.selectionCarrierNo ||
          list[i].werks !== this.selecttionWerks ||
          (this.selecttionChargeText === "运杂费"
            ? list[i].chargeText !== "运杂费"
            : list[i].chargeText === "运杂费")
        ) {
          this.$refs.crud.$refs.table.toggleRowSelection(list[i], false);
        }
      }
    },
    //排序
    sortChange(val) {
      switch (val.prop) {
        case "money":
          if (val.order === "ascending") {
            this.data = this.data.sort((a, b) => {
              return a.money - b.money;
            });
          } else if (val.order === "descending") {
            this.data = this.data.sort((a, b) => {
              return b.money - a.money;
            });
          } else {
            this.refreshChange();
          }
          break;
      }
    },
    //导出
    exportExcel() {
      var url = `/api/blade-tms/statementorder/settlementSheet/export?Blade-Auth=bearer ${getToken()}&${serialize(
        this.searchForm
      )}`;
      this.downFile(url);
    },
  },
};
</script>

<style></style>
